package com.dmgis.hndz.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.*;
import java.util.UUID;

@Slf4j
public class FileUtil {

    /**
     * 读取文件内容
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String readLine(File file) throws IOException {
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
        BufferedReader bufferedReader = new BufferedReader(read);

        StringBuilder lineTxt = new StringBuilder();
        String text;
        while ((text = bufferedReader.readLine()) != null) {
            lineTxt.append(text);
        }
        return lineTxt.toString();
    }

    /**
     * 获取文件路径
     *
     * @param suffix
     * @return
     */
    public static File getFile(String suffix) {
        String fileName = UUID.randomUUID() + "." + suffix;
        File file = new File(getFilePath(), fileName);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            boolean mkdir = parentFile.mkdir();
            log.info(mkdir ? "文件夹创建成功" : "文件夹创建失败");
        }
        return file;
    }

    /**
     * 获取文件夹路径
     *
     * @return
     */
    private static String getFilePath() {
        ApplicationHome applicationHome = new ApplicationHome(FileUtil.class);
        return applicationHome.getDir().getParentFile()
                .getParentFile().getAbsolutePath() + "\\src\\main\\resources\\file";
    }

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //将Byte数组转换成文件
    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * bytes转文件
     *
     * @param buffer
     * @param filePath
     * @author dsq
     * @date 2018年8月16日
     */
    public static void bytesToFile(byte[] buffer, final String filePath) {

        File file = new File(filePath);
        OutputStream output = null;
        BufferedOutputStream bufferedOutput = null;

        try {
            output = new FileOutputStream(file);

            bufferedOutput = new BufferedOutputStream(output);

            bufferedOutput.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedOutput) {
                try {
                    bufferedOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //设置可执行权限
            boolean exec = file.setExecutable(true, false);
            //设置可读权限
            boolean read = file.setReadable(true, false);
            //设置可写权限
            boolean write = file.setWritable(true, false);
        }

    }

    public static void ASNItoUTF(String filePath1, String filePath) throws IOException {
        File file = new File(filePath1);

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));//转成gbk就不乱码了

        /* 写入Txt文件 */
        File writename = new File(filePath); // 相对路径，如果没有则要建立一个新的output。txt文件
        writename.createNewFile(); // 创建新文件
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(writename), "UTF-8"));

        String line = null;
        while ((line = br.readLine()) != null) { // 阻塞
            bw.write(line);
            bw.newLine();
            bw.flush();
            System.out.println(line);
        }

        br.close();
        bw.close();

        //设置可执行权限
        boolean exec = writename.setExecutable(true, false);
        //设置可读权限
        boolean read = writename.setReadable(true, false);
        //设置可写权限
        boolean write = writename.setWritable(true, false);
    }

}
